import {
  Controller,
  Get,
  HttpException,
  HttpStatus,
  Query,
  SetMetadata,
  UseFilters,
  UseGuards,
  UseInterceptors,
  UsePipes,
} from '@nestjs/common';
import { AppService } from './app.service';
import { Roles } from './common/decorator/Roles';
import { AppExceptionFilter } from './common/filters/app-exception.filter';
import { AppGuard } from './common/guards/app.guard';
import { AppInterceptor } from './common/interceptors/app.interceptor';
import { AppPipe } from './common/pipes/app.pipe';

// @UseFilters(AppExceptionFilter)
@UsePipes(new AppPipe('controller'))
@SetMetadata('admin-controller', ['admin-controller'])
@SetMetadata('test-controller', ['test-controller'])
@UseGuards(AppGuard)
@UseInterceptors(new AppInterceptor('app-controller'))
@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}
  // AppMethodsPipe
  @UsePipes(new AppPipe('methods'))
  @Get('/pi')
  getHelloPi(
    @Query(new AppPipe('Query')) query: any,
    @Query('page') page: string,
    @Query('size') size: string,
    // @Query('size', new AppPipe('size param')) size: string,
  ): string {
    console.log(query);
    console.log(page, size);
    return this.appService.getHello();
  }

  @UseInterceptors(new AppInterceptor('getHello'))
  @Roles('admin')
  @SetMetadata('test', ['testVal'])
  @UseGuards(AppGuard)
  @Get()
  getHello(@Query('page') page: string, @Query('size') size: string): string {
    console.log(page, size);
    return this.appService.getHello();
  }

  @UseFilters(AppExceptionFilter)
  @Get('/app/exc')
  getHelloException(): string {
    throw new HttpException({ msg: 'Forbidden' }, HttpStatus.FORBIDDEN);
  }

  @Get('/exc/global')
  getHelloExceptionGlobal(): string {
    throw new HttpException({ msg: 'Forbidden' }, HttpStatus.FORBIDDEN);
  }
}
